﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using jSLApp.Client.MainService;
using jSLApp.Client.Security;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;
using jSLCore.Client.Presentation.BaseControl.Selection;
using jSLCore.Client.Presentation.jBinding.BaseValidationRules;
using jSLCore.Utility.Extensions;

namespace jSLApp.Client.Forms
{
    public partial class GenreList : SelectControl
    {
        public GenreList()
        {
            InitializeComponent();
        
        }
        
        public override void LoadInitialData()
        {
            DoWork("Загрузка жанров",(sender, args) =>
                                         {
                                             args.Result = Communication.Proxy.GetGenres();
                                         },(o, eventArgs) =>
                                               {
                                                   if(eventArgs.Error==null)
                                                   {
                                                       Genres =
                                                           ((IEnumerable<Genre>) eventArgs.Result).
                                                               ToObservableCollection();
                                                   }
                                               });
        }

        public ObservableCollection<Genre> Genres
        {
            get { return (ObservableCollection<Genre>) grid.ItemsSource; }
            set { grid.ItemsSource = value; }
        }

        #region dialog

        public override ISelectableGrid GetSelectableGrid()
        {
            return grid;
        }

        public override bool IsOkVisibleWhenNotSelectable
        {
            get { return false; }
        }

        #endregion

        private void addGenre_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new jAskForStringBox("Название",string.Empty,false,new jNotEmptyStringRule(),null);
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   var str = (string)data;
                                   DoWork("Создание жанра",(sender1, args) =>
                                                               {
                                                                   args.Result = Communication.Proxy.AddGenre(str);
                                                               },(o1, eventArgs) =>
                                                                     {
                                                                         if (eventArgs.Error == null)
                                                                         {
                                                                             Genres.Insert(0, (Genre) eventArgs.Result);
                                                                         }
                                                                     });
                               });
        }

        private void deleteGenre_Clicked(object sender, RowSelectedEventArgs e)
        {
                var genre = (Genre) e.RowObject;
                DoWork("Удаление жанра", (o, args) => Communication.Proxy.DeleteGenre(args.Int), (sender1, eventArgs) =>
                                                                                                     {
                                                                                                         if (
                                                                                                             eventArgs.
                                                                                                                 Error ==
                                                                                                             null)
                                                                                                         {
                                                                                                             Genres.
                                                                                                                 Remove(
                                                                                                                     genre);
                                                                                                         }
                                                                                                     }, genre.id);
            
        }
    }
}
